<?php

namespace Core\init;

use Core\annotations\Bean;
use Core\lib\DbPool;

/**
 * @Bean
 */
class PdoPool extends DbPool
{
    public function __construct($min = 5, $max = 10)
    {
        global $GLOBALS_CONFIGS;
        if (!isset($GLOBALS_CONFIGS['db_pool']) || empty($GLOBALS_CONFIGS['db_pool'])) {
            throw new \Exception("未设置连接池配置参数");
        }
        $config = $GLOBALS_CONFIGS['db_pool'];
        $min = $config['min'];
        $max = $config['max'];
        $isDelTime = $config['isdeltime'];
        parent::__construct($min, $max, $isDelTime);
//        协程化
        \Swoole\Runtime::enableCoroutine(true);
    }

    /**
     * 获取PDO 实例
     * @return mixed|\PDO
     * @throws \Exception
     */
    function newDb()
    {
        global $GLOBALS_CONFIGS;
        if (!isset($GLOBALS_CONFIGS['db']) || !isset($GLOBALS_CONFIGS['db']['default'])) {
            throw new \Exception("数据库配置异常");
        }
        $config = $GLOBALS_CONFIGS['db']['default'];
        $dsn = "{$config['driver']}:host={$config['host']};dbname={$config['database']}";
        $pdo = new \PDO($dsn, $config['username'], $config['password']);
        return $pdo;
    }

}
